define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pack/station/index' + location.search,
                    add_url: 'pack/station/add',
                    edit_url: 'pack/station/edit',
                    del_url: 'pack/station/del',
                    multi_url: 'pack/station/multi',
                    import_url: 'pack/station/import',
                    table: 'pack_station',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: false},
                        {field: 'codeimage', title: __('站点二维码'), events: Table.api.events.image, operate: false,
                            formatter: function(value, row){
                                if(value){
                                    value = '<a href="javascript:"><img class="img-md img-center" src="'+value+'"></a><a href="javascript:" class="text-info ercode" data-href="/root.php/pack/station/qrcode/ids/'+row.id+'">重新生成二维码</a>'
                                    ;
                                }else{
                                    value = '<a href="javascript:" data-href="/root.php/pack/station/qrcode/ids/'+row.id+'" class="text-info ercode">生成二维码</a>';
                                }
                                return value;
                            }
                        },
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $("#table").on("click",".ercode",function () {
                var url = $(this).data('href');
                Fast.api.ajax( url,
                    function(data,ret){
                        setTimeout(function () {
                            location.reload();
                        }, 1500);
                    }
                );
            })

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});